<?php
namespace App\Job;

use Hyperf\AsyncQueue\Annotation\AsyncQueueMessage;

use App\Model\User;
use App\Repository\UserRepository;

// 最小区业绩分红
class UserMinRedJob
{
    /**
     * @AsyncQueueMessage
     */
    public function push($ids, $money)
    {
        $users = User::find($ids);
        
        // 算出所有团队业绩总额
        $moneyUser = $users->sum('money_user');
        
        // 算出每份额
        $one = bcdiv($moneyUser, $money, 2);
        
        // 循环
        foreach($users as $user)
        {
            $this->red($user, $one);
        }
    }
    
    // 分红
    public function red($user, $one)
    {
        // 用户团队业绩算出每份
        $money = bcdiv($user->money_user, $one, 2);
        
        // 用户
        $user = UserRepository::make($user);
        
        // 加入奖
        $user->moneyFullAdd($money, '最小区业绩分红', 'user_min_prize');
    }
}